file(GLOB_RECURSE SRC_LIST "*.cpp")
file(GLOB_RECURSE HEADERS "*.h")
file(GLOB_RECURSE QRC_LIST "*.qrc")
file(GLOB_RECURSE UI_LIST "*.ui")
foreach (qrc_file ${QRC_LIST})
    message(STATUS "Adding resource file: ${qrc_file}")
    qt_add_resources(SRC_LIST "${qrc_file}")
endforeach ()

set(MODULE_NAME gui)
qt_add_library(${MODULE_NAME} SHARED ${SRC_LIST} ${HEADERS} ${UI_LIST})
target_include_directories(${MODULE_NAME} PRIVATE ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${MODULE_NAME} PUBLIC
        Qt6::Core Qt6::Gui Qt6::Widgets Qt6::Charts ${OpenCV_LIBS} app
)
add_dependencies(${MODULE_NAME} app BuildInfo.h)
target_compile_definitions(${MODULE_NAME} PRIVATE LIBGUI_LIBRARY)
target_sources(${MODULE_NAME} PRIVATE ${UI_LIST})

if (WIN32)
    if (DEFINED Qt6_DIR)
        get_filename_component(QT_BIN_DIR "${Qt6_DIR}/../../../bin" ABSOLUTE)
        set(DEPLOYQT_COMMAND "${QT_BIN_DIR}/windeployqt")
    else ()
        find_program(DEPLOYQT_COMMAND windeployqt)
    endif ()
else ()
    find_program(DEPLOYQT_COMMAND linuxdeployqt)
endif ()

if (NOT DEPLOYQT_COMMAND)
    message(FATAL_ERROR "win/linux deployqt not found. Please set Qt6_DIR to the Qt installation directory.")
else ()
    add_custom_command(TARGET ${MODULE_NAME} POST_BUILD
            COMMAND ${DEPLOYQT_COMMAND} $<TARGET_FILE:${MODULE_NAME}>
            WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
            COMMENT "Running deployqt to handle dependencies..."
    )
endif ()


